function toggleMainLeft() {
	var mainLeft = document.getElementsByClassName('main-left')[0];
	if (mainLeft.classList.contains('open')) {
		mainLeft.classList.add('close');
		mainLeft.classList.remove('open');
	}
	else {
		mainLeft.classList.add('open');
		mainLeft.classList.remove('close');
	}
}


function markA(a) {
	a.classList.add('current-index');
	var ul = a.parentElement.querySelector('ul');
	if (ul != undefined) {
		ul.classList.add('list-open');
	}
	var elem = a;
	while (! elem.classList.contains('h-list')) {
		if (elem.tagName == 'UL') {
			elem.classList.add('list-open');
		}
		elem = elem.parentElement;
	}
}


function removeOldMark() {
	var aElems = document.querySelectorAll('.h-list a.current-index');
	for (var idx = 0; idx < aElems.length; idx += 1) {
		var aElem = aElems[idx];
		aElem.classList.remove('current-index');
	}
	var uls = document.querySelectorAll('.h-list ul.list-open');
	for (var idx = 0; idx < uls.length; idx += 1) {
		var ul = uls[idx];
		ul.classList.remove('list-open');
	}
}


function updateList() {
	var hs = document.querySelectorAll('#docman-hook-markdown h2,h3,h4,h5,h6');
	var lastH = undefined;
	for (var idx = 0; idx < hs.length; idx += 1) {
		var h = hs[idx];
		if (h.getBoundingClientRect().top > parseInt(getComputedStyle(document.documentElement).getPropertyValue('--header-height')) + 100) {
			break;
		}
		lastH = h;
	}
	if (lastH == undefined) {
		lastH = hs[0];
	}
	if (lastH != undefined) {
		var aElems = document.querySelectorAll(`.h-list a[href="#${lastH.id}"]`);
		removeOldMark();
		aElems.forEach(markA);
	}
}

function toggleMainLeftTab(tab) {
	var target = document.querySelector('.main-left-inner')
	for (var idx = target.classList.length - 1; idx > -1; idx -= 1) {
		var cls = target.classList[idx]
		if (cls.startsWith('current-')) {
			target.classList.remove(cls)
		}
	}
	target.classList.add(`current-${tab}-tab`)
}


window.onload = function() {
	document.getElementById('mirror-docman-hook-list').innerHTML = document.getElementById('docman-hook-list').innerHTML;
	updateList();
	window.onscroll = updateList;
};
